package com.it.springbootspringdatajpa.dao;

import com.it.springbootspringdatajpa.domain.Contract;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @projectName: springboot-spring-data-jpa
 * @package: com.it.springbootspringdatajpa.dao
 * @className: ContractRepository
 * @author: Navid
 * @description: TODO
 * @date: 2021/10/23 17:48
 * @version: 1.0
 */
@Repository
public interface ContractRepository extends JpaRepository<Contract, Integer> {

    @Query("select c from Contract c")
    List<Contract> findAllByOther();

    @Modifying
    @Query("update Contract c set c.contactName=?1 where c.id=?2")
    void updateNameById(String name, Integer id);

    @Query("select c from Contract c where c.contractNo=?1 and c.contractType=?2 and c.contactName=?3")
    Contract findByCondition(String contractNo, String contractType, String contactName);

    @Query("select c from Contract c where c.contractStatus = ?1 and DATE_FORMAT(now(),'%m' ) = DATE_FORMAT(c.beginTime,'%m' )")
    List<Contract> findByContractStatus(boolean contractStatus);

    @Query(nativeQuery = true, value = "select * from tt_contract where tt_contract.contract_no = ?3  limit ?1,?2")
    List<Contract> findByPage(int startIndex, int PageSize, String contractNo);
}
